package com.zy.web.controller.business;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zy.business.domain.TbAbsence;
import com.zy.business.domain.TbAbsenceAudit;
import com.zy.business.domain.TbReportBack;
import com.zy.business.domain.vo.AbsenceDto;
import com.zy.business.mapper.AbsenceAuditMapper;
import com.zy.business.mapper.ReportBackMapper;
import com.zy.common.annotation.Log;
import com.zy.common.annotation.RepeatSubmit;
import com.zy.common.core.controller.BaseController;
import com.zy.common.core.domain.AjaxResult;
import com.zy.common.core.domain.entity.SysUser;
import com.zy.common.core.page.TableDataInfo;
import com.zy.common.enums.BusinessType;
import com.zy.common.utils.SecurityUtils;
import com.zy.business.service.AbsenceService;
import com.zy.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @Description:请假申请控制器
 * @Author GIGABYTE2
 * @Date 2020/11/6
 */
@Api(tags = "请假管理接口")
@RestController
@RequestMapping("/absence")
public class AbsenceController extends BaseController {

    @Autowired
    private AbsenceService absenceService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ReportBackMapper reportBackMapper;

    @Autowired
    private AbsenceAuditMapper absenceAuditMapper;

    @GetMapping("/list")
    @ApiOperation(value = "获取请假申请列表(获取审批列表时type:copy:抄送;approve:审批;my:我的)")
    public TableDataInfo list(AbsenceDto absenceDto) {
        startPage();
        List<TbAbsence> absenceList = absenceService.list(absenceDto);
        return getDataTable(absenceList);
    }

    @GetMapping("/getDetail")
    @ApiOperation(value = "根据id获取详情")
    public AjaxResult getDetail(@RequestParam Long absenceId,Long approveId){
        TbAbsence absence = absenceService.getDetail(absenceId);
        if (approveId!=null){
            TbAbsenceAudit audit = absenceAuditMapper.selectOne(new QueryWrapper<TbAbsenceAudit>().eq("absence_id",absenceId).eq("approve_id",approveId));
            if (audit==null || absence==null){
                return AjaxResult.error("该条请假申请已撤回");
            }
        }
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("absence",absence);
        List<TbAbsenceAudit> auditList = absenceAuditMapper.selectAuditList(absenceId);
        dataMap.put("auditList",auditList);
        TbReportBack reportBack = reportBackMapper.selectOne(new QueryWrapper<TbReportBack>().eq("absence_id", absenceId));
        if (reportBack!=null){
            dataMap.put("reportBack",reportBack);
        }
        return AjaxResult.success(dataMap);
    }


    @PostMapping
    @Log(title = "请假申请", businessType = BusinessType.INSERT)
    @ApiOperation(value = "添加请假申请")
    @RepeatSubmit
    public AjaxResult add(@RequestBody TbAbsence absence) {
        absence.setApplyId(SecurityUtils.getUserId());
        absence.setCreateBy(getUsername());
        return absenceService.add(absence);
    }

    @PostMapping("/approve")
    @Log(title = "请假申请", businessType = BusinessType.AUDIT)
    @ApiOperation(value = "审批请假申请")
    @RepeatSubmit
    public AjaxResult approve(@RequestBody TbAbsenceAudit absenceAudit){
        absenceAudit.setUpdateBy(SecurityUtils.getUsername());
        absenceAudit.setUpdateTime(new Date());
        return absenceService.approve(absenceAudit);
    }

    @PutMapping
    @Log(title = "请假申请", businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改请假申请")
    public AjaxResult edit(@RequestBody TbAbsence absence){
        absence.setUpdateBy(getUsername());
        return absenceService.edit(absence);
    }

    @DeleteMapping("/{absenceId}")
    @Log(title = "请假申请", businessType = BusinessType.DELETE)
    @ApiOperation(value = "删除请假申请")
    public AjaxResult delete(@PathVariable Long absenceId){
        this.absenceService.removeById(absenceId);
        return AjaxResult.success();
    }

    @ApiOperation(value = "获取用户的剩余加班时长")
    @GetMapping("/getOverworkHour")
    public AjaxResult getOverworkHour(@RequestParam(name = "userId") Long userId){
        Map<String, Object> data = new HashMap<>();
        SysUser byId = userService.selectUserById(userId);
        data.put("overworkHour",byId.getOverworkHour());
        return AjaxResult.success(data);
    }

    @ApiOperation(value = "根据请假时间计算请假时长")
    @GetMapping("/getDuration")
    public AjaxResult getDuration(String startTime, String endTime){
        String duration = absenceService.getDuration(startTime,endTime);
        return AjaxResult.success().put("duration",duration);
    }

    @ApiOperation(value = "销假")
    @PostMapping("/reportBack")
    @RepeatSubmit
    public AjaxResult reportBack(@RequestBody TbReportBack reportBack){
        return toAjax(absenceService.reportBack(reportBack));
    }

    @ApiOperation(value = "修改销假申请")
    @PostMapping("/editReportBack")
    public AjaxResult editReportBack(@RequestBody TbReportBack reportBack){
        return toAjax(absenceService.editReportBack(reportBack));
    }

    @ApiOperation(value = "审批销假申请")
    @PostMapping("/approveReportBack")
    @RepeatSubmit
    public AjaxResult approveReportBack(@RequestBody TbReportBack reportBack){
        return toAjax(absenceService.approveReportBack(reportBack));
    }

}
